package com.navinfo.platform.dataanalysis.service.impl;

import com.navinfo.platform.common.service.ILoadDataChannel;
import com.navinfo.platform.common.service.ILoadDataService;
import com.navinfo.platform.common.service.impl.LoadDataFromHive;
import com.navinfo.platform.common.utils.DateUtils;
import org.apache.spark.sql.SparkSession;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 加载故障日统计信息
 * @author  web 20191203
 */
public class FaultDataLoadService implements ILoadDataService {
    private ILoadDataChannel hiveChannel = LoadDataFromHive.INSTANCE;

    @Override
    public List<Object> loadData(SparkSession spark, Map<String, String> configMap) {
        Tuple2<String, String> startEndDay = DateUtils.getStartEndDay(configMap.get("args_1"), configMap.get("args_2"));
        String hql1 = configMap.get("hive.fault.car.hql");
        hql1 = String.format(hql1, startEndDay._1, startEndDay._2);


        System.out.println("查询按车辆类型的故障日统计信息的hql："+ hql1);
        long start1 = System.currentTimeMillis();
        List<Object> data1 = hiveChannel.readData(spark, hql1);
        long start2 = System.currentTimeMillis();
        System.out.println("加载按车辆类型的故障日统计信息，耗时"+ (start2-start1));


        String hql2 = configMap.get("hive.fault.engine.hql");
        hql2 = String.format(hql2, startEndDay._1, startEndDay._2);
        System.out.println("查询按发动机类型的故障日统计信息的hql："+ hql2);
        List<Object> data2 = hiveChannel.readData(spark, hql2);
        long start3 = System.currentTimeMillis();
        System.out.println("加载按车辆类型的故障日统计信息，耗时"+ (start3-start2));


        String hql3 = configMap.get("hive.fault.times.hql");
        hql3 = String.format(hql3, startEndDay._1, startEndDay._2);
        System.out.println("查询故障维度日统计信息的hql："+ hql3);
        List<Object> data3 = hiveChannel.readData(spark, hql3);
        long start4 = System.currentTimeMillis();
        System.out.println("加载按车辆类型的故障日统计信息，耗时"+ (start4-start3));

        return Arrays.asList(data1,data2,data3);
    }

}
